/**
 *
 */
import axios, { type AxiosInstance } from "axios";
import {getToken} from "@/utils/store";
import { ElMessage } from "element-plus";
import router from "@/router";

class Base {
  instance: AxiosInstance;
  constructor(baseUrl?: string) {
    const instance = axios.create();
    instance.defaults.baseURL =
      baseUrl || import.meta.env.SYSTEM_BASE_API || "";
    // instance.defaults.withCredentials = true;
    instance.interceptors.request.use(
      (config) => {
        config.headers.Authorization = getToken();
        config.headers["Content-Type"] = config.headers["Content-Type"] || "application/json; charset=utf-8";
        return config;
      },
      (error) => {
        return Promise.reject(error);
      }
    );
    instance.interceptors.response.use(
      (response) => {
        // 如果是下载
        if (response.data instanceof Blob) {
          return response.data;
        }
        // 如果返回成功
        if (response.data && response.data.code === 0) {
          return response.data.data;
        }
        // 如果返回未登录
        if (response.data && response.data.code === 3) {
          //   userStore.confirmLogout("登录过期，请重新登录")
          router.replace("/login")
          return Promise.reject(response.data);
        }

        return Promise.reject(response.data);
      },
      (error) => {
        return Promise.reject(error);
      }
    );
    this.instance = instance;
  }

  get(
    url: string,
    params: any = {},
    options: APIOptions = { isToken: true }
  ): Promise<any> {
    return this.instance({
      method: "get",
      url: url,
      params: params,
    });
  }
  post(
    url: string,
    data?: any,
    options: APIOptions = { isToken: true }
  ): Promise<any> {
    return this.instance({
      method: "post",
      url: url,
      data: data,
    });
  }
  patch(
    url: string,
    data?: any,
    options: APIOptions = { isToken: true }
  ): Promise<any> {
    return this.instance({
      method: "patch",
      url: url,
      data: data,
    });
  }
  patchForm(
    url: string,
    data?: any,
    options: APIOptions = { isToken: true }
  ): Promise<any> {
    return this.instance({
      method: "patch",
      url: url,
      data: data,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded"
      }
    });
  }
  postForm(
    url: string,
    data?: any,
    options: APIOptions = { isToken: true }
  ): Promise<any> {
    return this.instance({
      method: "post",
      url: url,
      data: data,
      headers: {
        "Content-Type": "application/x-www-form-urlencoded"
      }
    });
  }
  delete(
    url: string,
    params: any = {},
    options: APIOptions = { isToken: true }
  ): Promise<any> {
    return this.instance({
      method: "delete",
      url: url,
      params: params,
    });
  }

  download(
    url: string,
    params: any,
    options: APIOptions = { isToken: true }
  ): Promise<any> {
    return this.instance({
      method: "get",
      url,
      params,
      responseType: "blob", // 防止乱码
    });
  }
  formdata(
    url: string,
    file: File,
    data: any,
    options: APIOptions = { isToken: true }
  ) {
    let formdata = new FormData();
    formdata.append("file", file);
    for (const iterator of Object.keys(data)) {
      formdata.append(iterator, data[iterator]);
    }
    return this.instance({
      method: "post",
      url,
      data: formdata,
    });
  }
}

export default Base;
